package cn.edu.wit.servlets;

import cn.edu.wit.beans.Clazz;
import cn.edu.wit.beans.Student;
import cn.edu.wit.dao.ClazzDAO;
import cn.edu.wit.dao.StudentDAO;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class StudentUpdateServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");

		// 1.��������
		String stuNum = request.getParameter("num");
		String stuName = request.getParameter("name");
		String stuSex = request.getParameter("sex");

		int stuAge = Integer.parseInt(request.getParameter("age"));

		String stuTel = request.getParameter("tel");
		String stuQQ = request.getParameter("qq");
		String stuAddr = request.getParameter("addr");
		String stuRemark = request.getParameter("remark");
		
		int oldCid = Integer.parseInt(request.getParameter("old_cid"));
		int cid = Integer.parseInt(request.getParameter("class_id"));
		
		Clazz cla = new Clazz();
		cla.setCid(cid);

		// 2.�����յ���ѧ����Ϣ���浽һ��Student������
		Student s = new Student(stuNum, stuName, stuSex, stuAge, stuTel, stuQQ,
				stuAddr, stuRemark,cla);
		
		//3.ִ���޸�
		boolean b = new StudentDAO().updateStudent(s);
		
		//�޸İ༶�������ϰ༶-1���°༶����+1
		if(oldCid!=cid){
			ClazzDAO cdao = new ClazzDAO();
			cdao.changeCount(oldCid, -1);
			cdao.changeCount(cid, 1);
		}
		
		//4.��ת����ʾҳ��
		String tips = b?"<label style='color:green'>ѧ����Ϣ�޸ĳɹ���</label>":"<label style='color:red'>ѧ����Ϣ�޸�ʧ�ܣ�</label>";
		request.setAttribute("tips", tips);
		request.getRequestDispatcher("prompt.jsp").forward(request, response);

	}

}
